<template>
	<view class="ys">
		<view class="info">
			<view class="infoli">
				<view class="left">
					店名
				</view>
				<view class="right">
					<input type="text" v-model="name" placeholder="请输入店名" :disabled="!allow_submit">
				</view>
			</view>
			<view class="infoli">
				<view class="left">
					行业
				</view>
				<view class="right">
					<uni-data-select :localdata="range" :value="industry_id" v-model="industry_id"
						:disabled="!allow_submit"></uni-data-select>
				</view>
			</view>
			<view class="infoli">
				<view class="left">
					营业执照
				</view>
				<view class="right">
					<image :src="show_license || '/static/index/yyzz.png'" @click="uploadImg" mode="widthFix">
					</image>
				</view>
			</view>
			<button v-if="!need_license" @click="applyStore" :loading="loading" :disabled="loading">{{text}}</button>
			<button v-else @click="updateLicense" :loading="loading" :disabled="loading">更新营业执照</button>
		</view>
	</view>
</template>

<script>
	import {
		getUserStore,
		applyStore,
		updateLicense
	} from "@/api/user.js"
	import {
		commonParam
	} from "@/api/api.js"
	export default {
		data() {
			return {
				domain: this.$config.websiteUrl,
				loading: false,
				name: '',
				license: '',
				show_license: '',
				store: [],
				need_license: false,
				allow_submit: true,
				text: '',
				range: [],
				industry_id: ''
			}
		},
		onLoad() {
			this.getUserStore()
			this.commonParam()
		},
		methods: {
			commonParam() {
				commonParam().then(res => {
					if (res.code == 0) {
						var arr = Object.values(res.data.industry)
						arr.forEach(item => {
							this.range.push({
								value: item.id,
								text: item.name
							})
						})
					}
				})
			},
			updateLicense() {
				this.loading = true
				updateLicense({
					license: this.license,
				}).then(res => {
					uni.$showMsg(res.msg)
					this.loading = false
					if (res.code == 0) {
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					}
				})
			},
			applyStore() {
				if (!this.allow_submit) {
					return
				}
				if (this.name == '') {
					return uni.$showMsg('请输入店名')
				}
				this.loading = true
				applyStore({
					name: this.name,
					license: this.license,
					industry_id: this.industry_id
				}).then(res => {
					uni.$showMsg(res.msg)
					this.loading = false
					if (res.code == 0) {
						setTimeout(() => {
							uni.navigateBack()
						}, 500)
					}
				})
			},
			getUserStore() {
				getUserStore().then(res => {
					if (res.code == 0) {
						if (res.data.store) {
							this.store = res.data.store
							this.name = this.store.name
							this.industry_id = this.store.industry_id
						}
						this.show_license = this.store.license_path
						this.license = this.store.license
						this.need_license = res.data.need_license
						this.allow_submit = res.data.allow_submit
						this.text = res.data.text
					}
				})
			},
			seeimg(url) {
				let urls = [url]
				uni.previewImage({
					urls
				})
			},
			uploadImg(type) {
				if (!this.need_license && !this.allow_submit) {
					return
				}
				var that = this
				uni.chooseImage({
					sourceType: ['album', 'camera'], //从相册选择
					count: 1,
					success: (res) => {
						let igmFile = res.tempFilePaths;
						uni.showLoading({
							mask: true,
							title: '加载中'
						})
						for (let i = 0; i < igmFile.length; i++) {
							uni.uploadFile({
								url: that.$config.requestUrl +
									'/common/upload',
								header: {
									'tokens': uni.getStorageSync('tokens')
								},
								formData: {
									accept: 'image',
								},
								filePath: igmFile[i],
								name: 'UploadFile',
								success: (res) => {
									uni.hideLoading()
									let imgRes = JSON.parse(res.data); //微信和头条支持
									if (imgRes.code == 0) {
										let imgData = imgRes.data
										let url = imgData.path
										this.show_license = url
										this.license = imgData.id
									} else {
										uni.showToast({
											title: res.msg,
											icon: "none"
										})
										return
									}
								},
								fail(err) {
									uni.hideLoading()
								}
							})
						}
					},
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f2f2f2;
		padding-top: 15px;
		padding-bottom: 10px;
	}

	.info {
		width: 96%;
		margin: 0px auto;
		box-sizing: border-box;

		button {
			width: 96%;
			height: 41px;
			border-radius: 20px;
			line-height: 41px;
			text-align: center;
			font-size: 15px;
			color: #fff;
			background: linear-gradient(0deg, $uni-bg-color, $uni-bg-color);
			margin: 0 auto;
			margin-top: 20px;
		}

		.infoli {
			display: flex;
			flex-direction: column;
			font-size: 14px;
			border-bottom: 1px solid #eee;
			padding: 18px 10px;
			box-sizing: border-box;
			background: #fff;
			margin-bottom: 10px;
			border-radius: 8px;

			.left {
				font-size: 15px;
				font-weight: bold;
			}

			.right {
				flex: 1;
				margin-top: 20px;

				image {
					width: 100%
				}

				input {
					font-size: 14px;
				}
			}
		}
	}
</style>